import axios, {AxiosError, AxiosRequestConfig} from "axios";
import {config} from "exceljs/index";
import {useUserStore} from "../store/modules/user.ts";

// 用户token信息
let userStore = localStorage.getItem("userStore");
let token = userStore != null ? JSON.parse(userStore).token : "";

// 添加调试信息
console.log('VITE_APP_BASE_API:', import.meta.env.VITE_APP_BASE_API);

const service = axios.create({
    baseURL: import.meta.env.VITE_APP_BASE_API,
    timeout: 100000,
    // 自定义请求头
    // headers: { "AccessToken": token },
    withCredentials: true
});

// Request接口请求拦截
service.interceptors.request.use((config: AxiosRequestConfig) => {
    const userStore = useUserStore()
    const token: string = userStore.token;
    // 自定义请求头
    if (token) {
        config.headers['AccessToken'] = token;
    }
    return config;
},(error: AxiosError) => {
    return Promise.reject(error)
})

export default service;